import { defineStore } from 'pinia'

// 所有类型定义都来自 preload/index.d.ts，无需重复定义

export const useAppStore = defineStore('app_store', {
  state: () => {
    return {
      user: {} as any,
      navbar: {
        leftShow: 'logo' as 'logo' | 'back', // logo:显示 logo 和 app名称 ，back:显示返回 和 标题名称
        title: '' as string, // 标题名称
        rightShow: 'none' as 'home' | 'back' | 'query' | 'none' | 'collect' // none:不显示，home:首页显示三个按钮，back:显示 返回首页，query: 带查询功能, collect: 收藏页面功能
      } as any,

      compareUsers: {} as any
    }
  },
  actions: {
    login(user: any) {
      this.user = user
      localStorage.setItem('user', JSON.stringify(user))
    },
    async logout() {
      // 清理前端状态
      this.user = {}
      localStorage.removeItem('user')
      localStorage.removeItem('authToken')
      localStorage.removeItem('rememberedUsername')
      
      // 调用主进程的logout服务
      try {
        const userService = (window as any).userService
        if (userService) {
          await userService.logout()
        }
      } catch (error) {
        console.error('主进程退出登录失败:', error)
        // 即使主进程退出失败，前端状态也要清理
      }
    },

    isLogin() {
      const user = localStorage.getItem('user')
      this.user = user ? JSON.parse(user) : {}
      return user ? true : false
    },
    setNavTitle(title: string) {
      this.navbar.title = title
    }
  }
})
